package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.constant.CacheConstants;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.entity.BasicCustomizeTableEntity;
import com.iwomy.secureplat.platform.service.BasicCustomizeTableService;
import org.springframework.cache.annotation.CacheEvict;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 自定义表格
 *
 * @author pig
 * @date 2024-01-12 11:26:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicCustomizeTable" )
@Tag(description = "basicCustomizeTable" , name = "自定义表格管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicCustomizeTableController {

    private final  BasicCustomizeTableService basicCustomizeTableService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicCustomizeTable 自定义表格
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R getBasicCustomizeTablePage(@ParameterObject Page page, @ParameterObject BasicCustomizeTableEntity basicCustomizeTable) {
        return basicCustomizeTableService.getBasicCustomizeTable(basicCustomizeTable);
    }


    /**
     * 通过id查询自定义表格
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicCustomizeTableService.getById(id));
    }

    /**
     * 新增自定义表格
     * @param basicCustomizeTable 自定义表格
     * @return R
     */
    @Operation(summary = "新增自定义表格" , description = "新增自定义表格" )
    @SysLog("新增自定义表格" )
    @PostMapping
	@CacheEvict(value = CacheConstants.CUSTOMIZE_TABLE, allEntries = true)
    public R save(@RequestBody BasicCustomizeTableEntity basicCustomizeTable) {
    	basicCustomizeTable.setTenantId(SecurityUtils.getUser().getTenantId());
		BasicCustomizeTableEntity wrapperEntity = new BasicCustomizeTableEntity();
		wrapperEntity.setTableName(basicCustomizeTable.getTableName());
		wrapperEntity.setUserId(basicCustomizeTable.getUserId());
		LambdaQueryWrapper<BasicCustomizeTableEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicCustomizeTableEntity basicAlarmContacts = basicCustomizeTableService.getOne(wrapper);
		if (basicAlarmContacts!=null){
			basicAlarmContacts.setTableContent(basicCustomizeTable.getTableContent());
			basicCustomizeTableService.updateById(basicAlarmContacts);
		}else {
			basicCustomizeTableService.save(basicCustomizeTable);
		}
        return R.ok();
    }

    /**
     * 修改自定义表格
     * @param basicCustomizeTable 自定义表格
     * @return R
     */
    @Operation(summary = "修改自定义表格" , description = "修改自定义表格" )
    @SysLog("修改自定义表格" )
    @PutMapping
	@CacheEvict(value = CacheConstants.CUSTOMIZE_TABLE, allEntries = true)
    public R updateById(@RequestBody BasicCustomizeTableEntity basicCustomizeTable) {
        return R.ok(basicCustomizeTableService.updateById(basicCustomizeTable));
    }

    /**
     * 通过id删除自定义表格
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除自定义表格" , description = "通过id删除自定义表格" )
    @SysLog("通过id删除自定义表格" )
    @DeleteMapping
	@CacheEvict(value = CacheConstants.CUSTOMIZE_TABLE, allEntries = true)
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicCustomizeTableService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicCustomizeTable 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BasicCustomizeTableEntity> export(BasicCustomizeTableEntity basicCustomizeTable,Long[] ids) {
        return basicCustomizeTableService.list(Wrappers.lambdaQuery(basicCustomizeTable).in(ArrayUtil.isNotEmpty(ids), BasicCustomizeTableEntity::getId, ids));
    }
}